<?php
require_once MOD_ROOT.$module.'/common.inc.php';

switch($action){
	
	
	default:
		
		$voteid = $voteid ? intval($voteid) : $subjectid;
		if($voteid)
		{
			$info = $admin_vote->check($voteid);
			if($dosubmit)
			{
				if(!is_array($info)) showmessage($LANG[$errmsg[$info]]);
				
				// 允许游客投票
				//if(!$info['allowguest'] && !$_userid) showmsg($LANG['anonymous_cant_not_vote'], url("?module=member&action=login&forward=$forward"));
			
				checkcode($checkcode,$info['enablecheckcode'],$forward);
				$newdata=array();
				if(!is_array($votedata)) showmessage($LANG['submit_data_invalid'].$LANG['operation_failure'],$forward);
			
				$_userinfo = is_array($vote_config['userinfo'])?($vote_config['userinfo']):array();
			
				if((!$_userinfo && is_array($userinfo))) showmessage($LANG['submit_data_invalid'].$LANG['operation_failure'],$forward);
				if(count($_userinfo)!=count($userinfo)) showmessage($LANG['submit_data_invalid'].$LANG['operation_failure'],$forward);
				$userinfo = is_array($userinfo)?$userinfo:array();
				foreach($userinfo as $key=>$val)
				{
					if($key=='sex') continue ;
					if(!isset($_userinfo[$key])) showmessage($LANG['submit_data_invalid'].$LANG['operation_failure'].$key,$forward);
					if($_userinfo[$key] && !trim($val)) showmessage($LANG[$key].$LANG['sign_invalid'] , $forward);
					check_vote_field($key,$val);
				}
			
				$optiondata = array();
				foreach($votedata as $sid=>$data)
				{
					$newdata[$sid]=array();
					$min=max(intval($subjects[$sid]['minval']),1);
					$max = intval($subjects[$sid]['maxval']);
					$max = $max<$min?count($subjects[$sid]['options']):$max;
					$count = count($votedata[$sid]);
			
					if($subjects[$sid]['ismultiple'])
					{
			
						if(!($count>=$min && $count<=$max))  showmessage($LANG['operation_failure'].'multi', $forward);
					}
			
					foreach($data as $optionid){
						if(!$admin_vote->check_optionid($optionid)) showmessage($LANG['operation_failure'], $forward);
						$optiondata[$sid][$optionid]=1;
					}
				}
			
				$admin_vote->vote_submit($voteid, array_keys($votedata), $optiondata, $userinfo);
				showmessage($LANG['vote_success'],"?module=vote&action=statistics&voteid=$voteid");
			}
			else
			{
				
				//if(!is_array($info) && $info<=0) showmsg($LANG[$errmsg[$info]], "?module=$module&voteid=$voteid");
				 
				$votable = TRUE;
				if(!is_array($info))
				{
					$votable = FALSE;
					$vote_msg=$LANG[$errmsg[$info]];
				}
				
				$vote_config = $admin_vote->get_vote($voteid);
				$subject_lists = $subjects = $admin_vote->get_subjects($voteid);
				if(!$vote_config)   showmsg($LANG['illegal_parameters'], $forward);
				
				extract($vote_config);
	
				$template =$template ? $template : 'vote';
				$head['title']=$LANG['vote_subject'].' '.$title.' - '.$SETTING['sitename'];
				
				if($action=='js')
				{
					if(!function_exists('ob_gzhandler')) ob_clean();
					ob_start();
				
					$template = ($template =='vote')?'vote_submit' : $template ;
					$embed = 1 ;
					include template('vote',$template);
					$data=ob_get_contents();
					ob_clean();
					exit(format_js($data));
				}
				include template('vote.html', 'vote');
			}
		}
		else
		{
			$head['title'] = $M['name'];
			include template('index.html','vote');
		}
		
	break;
	
	/*统计结果*/
	case 'statistics':
		
		$voteid = intval($voteid);

		if(!$voteid) showmessage($LANG['illegal_parameters']);
		$info = $admin_vote->check($voteid);
		
		if($info['viewgroups']) $info['viewgroups'] = explode(',', $info['viewgroups']);
		if(is_array($info['viewgroups']) && !in_array($_groupid,$info['viewgroups'])) showmessage($LANG['viewgroup_disabled'], $forward);
		
		@extract($admin_vote->get_vote($voteid));
		$head['title'] = $LANG['vote_result'].':'.$subject.'-'.$SETTING['sitename'];
		if($parentid || !$subject) showmessage($LANG['illegal_parameters']);
		if($action == 'js')
		{
			$template = 'vote_show';
			ob_start();
			vote_show($template, $voteid, 1);
			$data = ob_get_contents();
			ob_clean();
			exit(format_js($data));
		}
		include template('statistics.html','vote');
	break;
}

//if(!$voteid) showmsg($LANG['illegal_parameters']);






function showmsg($msg,$forward='') {
    global $action;
    if($action=='js')
        echo format_js($msg);
    else
        showmessage($msg, $forward);
}
?>